package com.cskaoyan.controller.admincontroller;

import com.cskaoyan.bean.po.MarketOrder;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.chaosbean.order.bo.ShipBO;
import com.cskaoyan.bean.chaosbean.order.vo.ChannelVO;
import com.cskaoyan.bean.chaosbean.order.vo.DetailVO;
import com.cskaoyan.service.adminservice.OrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 楚夏三分
 * @datetime 2022/12/28 16:44
 * @description: TODO: 订单Controller层
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    /**
     * TODO: 获取order列表
     * @param info 分页信息
     * @param start 订单时间start范围
     * @param end 订单时间end范围
     * @param orderSn 订单编号
     * @param userId 用户id
     * @param orderStatusArray 订单状态
     * @return
     */
    // http://182.92.235.201:8083/admin/order/list?page=1&limit=20&sort=add_time&order=desc&start=&end=
    @RequiresPermissions("admin:order:list")
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date end,
                           String orderSn,
                           String userId,
                           Short[] orderStatusArray){
        CommonData<MarketOrder> data = orderService.list(info,start,end,orderSn,userId,orderStatusArray);
        return BaseRespVo.ok(data);
    }

    /**
     * TODO: 根据订单id查询订单详细信息
     * @param id 订单id
     * @return
     */
    // http://localhost:8083/admin/order/detail?id=1
    @RequiresPermissions("admin:order:detail")
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){
        DetailVO detailVO = orderService.detail(id);
        return BaseRespVo.ok(detailVO);
    }

    /**
     * TODO: 修改订单状态
     * @return 成功
     */
    // http://182.92.235.201:8083/admin/order/ship
    @RequiresPermissions("admin:order:ship")
    @RequestMapping("ship")
    public BaseRespVo ship(@RequestBody ShipBO shipBO){
        orderService.ship(shipBO);
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * TODO: 获得所有快递公司code简称和name全称
     * @return BaseRespVo
     */
    @RequiresPermissions("admin:order:channel")
    @RequestMapping("channel")
    public BaseRespVo channel(){
        List<ChannelVO> channelVOList = orderService.channel();
        return BaseRespVo.ok(channelVOList);
    }

    @RequiresPermissions("admin:order:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        Object orderId = map.get("orderId");
        System.out.println(orderId);
        orderService.deleteOrder((Integer)orderId);
        return BaseRespVo.ok();
    }

    @RequiresPermissions("admin:order:refund")
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        Double refundMoney = (Double) map.get("refundMoney");
        orderService.refund(orderId,refundMoney);
        return BaseRespVo.ok();
    }

}
